package com.tqjc.system.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.common.entity.BO.SysMaterialInfoBO;
import com.tqjc.system.system.dao.SysMaterialManageDao;
import com.tqjc.system.system.entity.SysGateRecordEntity;
import com.tqjc.system.system.entity.SysMaterialInfoEntity;
import com.tqjc.system.system.service.SysMaterialManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86195
 * @version 1.0
 * @description TODO
 * @date 2023/11/7 14:24
 */
@Service
@Slf4j
public class SysMaterialManageServiceImpl extends ServiceImpl<SysMaterialManageDao, SysMaterialInfoEntity> implements SysMaterialManageService{

    @Override
    public IPage<SysMaterialInfoEntity> getListByPage(IPage<SysMaterialInfoEntity> page, SysMaterialInfoBO sysMaterialInfoBO) {

        LambdaQueryWrapper<SysMaterialInfoEntity> wrapper = new LambdaQueryWrapper<SysMaterialInfoEntity>()
                .eq(StringUtils.isNoneBlank(sysMaterialInfoBO.getMaterialNo()), SysMaterialInfoEntity::getMaterialNo, sysMaterialInfoBO.getMaterialNo())
                .eq(StringUtils.isNoneBlank(sysMaterialInfoBO.getMaterialName()), SysMaterialInfoEntity::getMaterialName, sysMaterialInfoBO.getMaterialName())
                .orderByDesc(SysMaterialInfoEntity::getUpdateDate);
        return super.page(page,wrapper);
    }

    @Override
    public SysMaterialInfoEntity queryById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void updateInfo(SysMaterialInfoBO sysMaterialInfoBO) throws Exception {
        SysMaterialInfoEntity materialInfoEntity = SysMaterialInfoEntity.builder().materialNo(sysMaterialInfoBO.getMaterialNo())
                .materialName(sysMaterialInfoBO.getMaterialName()).idKey(Long.valueOf(sysMaterialInfoBO.getIdKey())).build();
        int i = baseMapper.updateById(materialInfoEntity);
        if(i <= 0){
            throw new Exception("物料信息更新失败");
        }
    }

    @Override
    public void addInfo(SysMaterialInfoBO sysMaterialInfoBO) throws Exception {
        SysMaterialInfoEntity materialInfoEntity = SysMaterialInfoEntity.builder().materialNo(sysMaterialInfoBO.getMaterialNo())
                .materialName(sysMaterialInfoBO.getMaterialName()).build();
        int i = baseMapper.insert(materialInfoEntity);
        if(i <=0 ){
            throw new Exception("新增物料信息失败");
        }
    }

    @Override
    public List<SysMaterialInfoEntity> getAllListByStatus(String status) {
        return baseMapper.selectList(new LambdaQueryWrapper<SysMaterialInfoEntity>()
                .eq(SysMaterialInfoEntity::getStatus, status));
    }
}
